//
//  CXLNewListController.m
//  WYNews
//
//  Created by 夜猫子 on 2017/3/18.
//  Copyright © 2017年 夜猫子. All rights reserved.
//

#import "CXLNewListController.h"
#import "CXLNetworkTool+News.h"
#import "CXLOneTableViewCell.h"
#import "CXLTwoTableViewCell.h"
#import "CXLThreeTableViewCell.h"
#import "CXLFourTableViewCell.h"
#import "CXLDetalViewController.h"

@interface CXLNewListController ()

@end

static NSString *oneCell = @"oneCell";
static NSString *twoCell = @"twoCell";
static NSString *threeCell = @"threeCell";
static NSString *fourCell = @"fourCell";

@interface CXLNewListController ()

@property (nonatomic, strong) NSMutableArray *listDataArrM;

@end

@implementation CXLNewListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self lsiLoad];
    
}

- (instancetype)initWithIndex:(NSInteger)index andModel:(CXLDetailsModel *)model{
    
    if (self = [super init]) {
        
        _model = model;
        _index = index;
        _listDataArrM = [NSMutableArray array];
    }
    
    return self;
}


- (void)setupUI {
    
    self.tableView.estimatedRowHeight = 300;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    [self.tableView registerClass:[CXLOneTableViewCell class] forCellReuseIdentifier:oneCell];
    [self.tableView registerClass:[CXLTwoTableViewCell class] forCellReuseIdentifier:twoCell];
    [self.tableView registerClass:[CXLThreeTableViewCell class] forCellReuseIdentifier:threeCell];
    [self.tableView registerClass:[CXLFourTableViewCell class] forCellReuseIdentifier:fourCell];
    
}


#pragma mark - UITableVieDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _listDataArrM.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CXLNewListModel *listModel = _listDataArrM[indexPath.row];
    
    if (listModel.hasHead) {
        CXLOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:oneCell forIndexPath:indexPath];
        
        cell.model = listModel;
        return cell;
    }
    
    if (listModel.imgextra) {
        
        CXLThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:threeCell forIndexPath:indexPath];
        
        cell.model = listModel;
        return cell;
    }
    
    if (listModel.imgType) {
        
        CXLFourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:fourCell forIndexPath:indexPath];
        cell.model = listModel;
        return cell;
    }
    
    CXLTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:twoCell forIndexPath:indexPath];
    
    cell.model = listModel;
    return cell;
}


/**
 加载数据
 */
- (void)lsiLoad {
    
    
    [[CXLNetworkTool sharedTool] requestNewList:_model.tid callBlock:^(NSArray *modelArr) {
        
        [_listDataArrM addObjectsFromArray:modelArr];
        
        [self.tableView reloadData];
    }];
    
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CXLDetalViewController *vc = [[CXLDetalViewController alloc]init];
    vc.model = _listDataArrM[indexPath.row];
    vc.view.backgroundColor = [UIColor whiteColor];
    
    
    
    
    
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
